﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MT5Collect
{
    public class ConfigTool
    {
        ///<summary>
        /// 读取配置文件
        ///</summary>
        public static Dictionary<string,string> GetConfig(string key="")
        {
            try
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                if (string.IsNullOrEmpty(key))
                {
                    foreach(var item in ConfigurationManager.AppSettings.Keys)
                    {
                        dic.Add(item.ToString(), ConfigurationManager.AppSettings[item.ToString()]);
                    }
                }
                else
                {
                    string value = ConfigurationManager.AppSettings[key];
                    dic.Add(key, value);
                }
                return dic;
            }
            catch (Exception)
            {

                throw;
            }
        }

        privatevoid FrmDirectSeeding_Load(object sender, EventArgs e)
        {
            this.GetConfig();
        }

        ///<summary>
        /// 保存配置文件
        ///</summary>
        privatevoid SaveConfig()
        {
            // 写入参数设置
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            configuration.AppSettings.Settings["WriteFileName"].Value = this.txtWriteFileName.Text;
            configuration.AppSettings.Settings["WritePath"].Value = this.txtWritePath.Text.Trim().Replace(Environment.NewLine, "|");
            configuration.AppSettings.Settings["PostMessageURL"].Value = this.txtPostMessageURL.Text;
            configuration.AppSettings.Settings["LeasedLineURL"].Value = this.txtLeasedLineURL.Text;
            configuration.Save();

            // 重新读取参数
            ConfigurationManager.RefreshSection("appSettings");
            WriteFile.WriteFileName = ConfigurationManager.AppSettings["WriteFileName"];
            WriteFile.WritePath = ConfigurationManager.AppSettings["WritePath"].Split('|');
            PostMessage.PostMessageURL = ConfigurationManager.AppSettings["PostMessageURL"];
            // PostMessage.LeasedLineURL = ConfigurationManager.AppSettings["LeasedLineURL"];
        }

        privatevoid btnSavaConfig_Click(object sender, EventArgs e)
        {
            // 保存设置
            SaveConfig();
        }
    }
}
    }
}
